import { createWebHistory, createRouter } from 'vue-router'

import HomeView from '../views/home/index.vue'
import Login from '../views/login.vue'
import Detail from '../views/detail.vue'
import Goods from '../views/goods.vue'
import Search from '../views/search.vue'
//二级路由
import Sy from '../views/home/sy.vue'
import Classify from '../views/home/classify.vue'
import Shopping from '../views/home/shopping.vue'
import Find from '../views/home/find.vue'
import My from '../views/home/my.vue'
const routes = [
    //重定向
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    { path: '/detail', component: Detail },
    { path: '/goods', component: Goods },
    { path: '/search', component: Search },
    {
        path: '/home',
        component: HomeView,
        children: [
            //重定向
            { path: '/home', redirect: '/home/sy' },
            { path: 'sy', component: Sy },
            { path: 'classify', component: Classify },
            { path: 'shopping', component: Shopping },
            { path: 'find', component: Find },
            { path: 'my', component: My },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})
//导航守卫
router.beforeEach((to, from, next) => {
    if (to.fullPath == '/home/my') {
        const token = localStorage.getItem('token')
        if (token) {
            next()
        }
        else {
            next('/login')
        }
    } else {
        next()
    }
})
export default router